<?php
 /******************************************************************************
 *   GunCMS is an avid supporter of open source software. This is the appropriate 
 *   option if you are creating an open source application with a license 
 *   compatible with the GNU GPL license v3.
 *   For details http://www.gnu.org/licenses/gpl.html
 *   DO NOT USE FOR COMMERCIAL WITHOUT PERMISSION
 *
 * GunCMS Version 1.0.3
 * Copyright(c) 2009-2010, T & L GdbR
 * info@guncms.de
 * 
 * http://www.guncms.de
 ********************************************************************************/
class FgcmsCustomer {
	var $db;
 	var $smarty;	
	public function __construct($db, $smarty) {
		//contructor function
		$this->db = $db;
		$this->smarty = $smarty;
	}
	
	/* Assigning the content related to the category_id to the 
	 * Smarty-Variable 'PAGE'. Because using ezSql, PAGE is a pointer!
	 * One should be aware of this fact, when PAGE is used in the 
	 * Smarty-Templates. Output is generated ba: {$PAGE->page_content}.
	 */ 
	public function process() {
		if(isset($_POST['act']) && $_POST['act']=='newcustomer')
			$this->newCustomer();
		else if(isset($_POST['act']) && $_POST['act']=='checkerror')
			$this->checkerror();	
		else if(isset($_POST['act']) && $_POST['act']=='editcustomer')
			$this->updateCustomer();		
		else if(isset($_POST['act']) && $_POST['act']=='login')
			$this->login();	
		else if(isset($_POST['act']) && $_POST['act']=='sendpassword')
			$this->forgotPassword();			
			
		if(isset($_GET['page']) && $_GET['page']=='customer_logout')
			$this->logout();
		else if(isset($_GET['page']) && $_GET['page']=='customer_myaccount'){
			if(isset($_SESSION['cust_id']) && $_SESSION['cust_id']!=""){
				$information = FgcmsCustomerBean::getCustomerById($this->db,$_SESSION['cust_id']);
				$information->customer_dob=	date('d.m.Y',strtotime($information->customer_dob));
				$this->smarty->assign('CUSTOMER',$information);	
			}
		}	
		else if(isset($_GET['page']) && $_GET['page']=='customer_active'){
			$this->activeCustomer();
		}	
		$allcountry = FgcmsCountryBean::get_all_country($this->db);	
		$this->smarty->assign('ALLCOUNTRY',$allcountry);
	}
	
	public function logout() {
		unset($_SESSION['cust_id']);
		unset($_SESSION['cust_login']);
		unset($_SESSION['cust_name']);
		unset($_SESSION['err']);
		
		setcookie("guncms[cust_id]", "");
		setcookie("guncms[cust_login]", "");
		setcookie("guncms[cust_name]", "");
			
		header("Location: ./?page=customer_login");
	}
	
	public function login() {
		$log = null;
		$pwd = null;
		unset($_SESSION['cust_id']);
		unset($_SESSION['cust_login']);
		unset($_SESSION['cust_name']);
		unset($_SESSION['err']);
		unset($_SESSION['file']);
		
		if($_POST['customer_email'] != "" && $_POST['customer_password'] != "") {
			$custommer = FgcmsCustomerBean::getCustomerByEmail($this->db, $_POST['customer_email']);
			if($custommer != null) {
				$passCrypted = $custommer->customer_password;
				if(crypt($_POST['customer_password'], $passCrypted) == $passCrypted) {
					$_SESSION['cust_id'] 		= $custommer->customer_id;
					$_SESSION['cust_login'] 	= $custommer->customer_email;
					$_SESSION['cust_name'] 	= $custommer->customer_firstname;
					//include this file to set locale ID to SESSION variable
					include_once("../localization.php");
					setcookie("guncms[cust_id]", $_SESSION['cust_id'], time()+3600);
					setcookie("guncms[cust_login]",$_SESSION['cust_login'], time()+3600);
					setcookie("guncms[cust_name]",$_SESSION['cust_name'], time()+3600);
				} else {
					$_SESSION['err'] = _("Invalid email address or password.");
				}
			} else {
				$_SESSION['err'] = _("Invalid email address or password.");
			}
		} else {
			if($_POST['customer_email_address'] == "" && $_POST['customer_password'] != "") {
				$_SESSION['err'] = _("Please type the email address.");
			} else if($_POST['customer_email_address'] != "" && $_POST['customer_password'] == "") {
				$_SESSION['err'] = _("Please type the password.");
			} else {
				$_SESSION['err'] = _("Please type the email address and password.");
			}
		}
		
		//Redirect to desire page
		if(!isset($_SESSION['err'])) {
			if($_POST['redirect_to'] == "") {
				ob_clean();
				header("Location: ./?page=checkout_shipping");
			} else {
				ob_clean();
				header("Location: ./?" . $_POST['redirect_to']);
			}
		} else {
			header("Location: ./?page=customer_login");
		}	
		
	}
	
	public function checkerror() {
		$ret = $this->validateCustomer();
		$type = array('ID' => -1, 'message' => $ret);
		echo array2json($type);	
	}
	
	public function newCustomer() {
			$type = array();
			$customer_gender				= "";
			$billing_gender				= "";
			$delivery_gender				= "";

			if(isset($_POST['customer_gender']))
				$customer_gender = $_POST['customer_gender'];
			/*if(isset($_POST['customer_firstname']))
				$customer_firstname = $_POST['customer_firstname'];
			if(isset($_POST['customer_lastname']))
				$customer_lastname = $_POST['customer_lastname'];*/
			if(isset($_POST['customer_dob']))
				$customer_dob = date('Y-m-d',strtotime($_POST['customer_dob']));
			if(isset($_POST['customer_email']))
				$customer_email = $_POST['customer_email'];
			if(isset($_POST['customer_telephone']))
				$customer_telephone = $_POST['customer_telephone'];
			if(isset($_POST['customer_cellphone']))
				$customer_cellphone = $_POST['customer_cellphone'];	
			if(isset($_POST['customer_fax']))
				$customer_fax = $_POST['customer_fax'];
			if(isset($_POST['customer_password']))
				$customer_password = $_POST['customer_password'];
			if(isset($_POST['customer_newsletter']))
				$customer_newsletter = $_POST['customer_newsletter'];
				
			if(isset($_POST['delivery_gender']))
				$delivery_gender = $_POST['delivery_gender'];	
			if(isset($_POST['delivery_firstname']))
				$delivery_firstname = $_POST['delivery_firstname'];
			if(isset($_POST['delivery_lastname']))
				$delivery_lastname = $_POST['delivery_lastname'];		
			if(isset($_POST['delivery_street_address']))
				$delivery_street_address = $_POST['delivery_street_address'];
			if(isset($_POST['delivery_postcode']))
				$delivery_postcode = $_POST['delivery_postcode'];
			if(isset($_POST['delivery_city']))
				$delivery_city = $_POST['delivery_city'];
			if(isset($_POST['delivery_state']))
				$delivery_state = $_POST['delivery_state'];
			if(isset($_POST['delivery_country_id']))
				$delivery_country_id = $_POST['delivery_country_id'];
				
			if(isset($_POST['billing_gender']))
				$billing_gender = $_POST['billing_gender'];	
			if(isset($_POST['billing_firstname']))
				$billing_firstname = $_POST['billing_firstname'];
			if(isset($_POST['billing_lastname']))
				$billing_lastname = $_POST['billing_lastname'];	
			if(isset($_POST['billing_street_address']))
				$billing_street_address = $_POST['billing_street_address'];
			if(isset($_POST['billing_postcode']))
				$billing_postcode = $_POST['billing_postcode'];
			if(isset($_POST['billing_city']))
				$billing_city = $_POST['billing_city'];
			if(isset($_POST['billing_state']))
				$billing_state = $_POST['billing_state'];
			if(isset($_POST['billing_country_id']))
				$billing_country_id = $_POST['billing_country_id'];	
			if(isset($_POST['customer_company']))
				$customer_company = @$_POST['customer_company'];
															
		
			
			$cust = array('customer_gender' => $customer_gender,
					  	  /*'customer_firstname' => $customer_firstname,
						  'customer_lastname' => $customer_lastname,*/
						  //'customer_dob' => $customer_dob,
						  'customer_email' => $customer_email,
						  'customer_telephone' => $customer_telephone,
						  'customer_cellphone' => $customer_cellphone,
						  'customer_fax' => $customer_fax,
						  'customer_password' => $customer_password,
						  'customer_newsletter' => $customer_newsletter,
						  'delivery_gender' => $delivery_gender,
						  'delivery_firstname' => $delivery_firstname,
						  'delivery_lastname' => $delivery_lastname,
						  'delivery_street_address' => $delivery_street_address,
						  'delivery_postcode' => $delivery_postcode,
						  'delivery_city' => $delivery_city,
						  'delivery_state' => $delivery_state,
					 	  'delivery_country_id' => $delivery_country_id,
						  'billing_gender' => $billing_gender,
						  'billing_firstname' => $billing_firstname,
						  'billing_lastname' => $billing_lastname,
						  'billing_street_address' => $billing_street_address,
						  'billing_postcode' => $billing_postcode,
						  'billing_city' => $billing_city,
						  'billing_state' => $billing_state,
					 	  'billing_country_id' => $billing_country_id,
					  	  'customer_company' => $customer_company);
			$ret = "";		
			$ret = $this->validateCustomer();
			if($ret['dem'] == 0) {					
				$id = FgcmsCustomerBean::saveCustomer($this->db, $cust);
				if($id > 0) {
					$type = array('ID' => 0);
					$host = $_SERVER['HTTP_HOST'].FOLDER_HOST;
					$code = crypt($to, '$ntp$');
					$from = FROMMAIL;
					$fromname = "T & L Tec GdbR";
					$subject = "GunWebshop Account Confirmation";
					$message = 	"Hello " . $customer_firstname . " " . $customer_lastname . "<br /><br />" .
								"Congratulations!You've just created an account on GunWebshop.<br />" . 
							   	"Please confirm your GunWebshop account by clicking here:<br /><br /><br />" .
							   	"http://$host/?page=customer_active&amp;code=" . $code . "&amp;id=" . $id . "<br /><br /><br />" .
							   	"If you're unable to click on the link above, copy and paste it into your browser's address bar.<br /><br /><br />" .
								"Sincerely,<br /><br />" . 
								"T & L Tec GdbR<br />" .
								"Plattlinger Strasse 5a<br />" .
								"93055 Regensburg<br />" .
								"Email: info@guncms.de<br />" .
								"Germany";
					$header = "Content-type: text/html\r\nFrom: $from\r\nReply-to: $from";
					FgcmsEmail::send_mail($customer_email, $message, $subject, $from, $fromname, false);
				}else{	
					$type = array('ID' => -1, 'message' => $ret);
					echo array2json($type);		
				}			
			} else {
				$type = array('ID' => -1, 'message' => $ret);
				echo array2json($type);	
			}
	}
	
	public function updateCustomer() {
			$type = array();
			$customer_gender				= "";
			$customer_firstname 			= "";
			$customer_lastname 				= "";
			$customer_dob 					= "";
			$customer_email_address 		= "";
			$customer_telephone			 	= "";
			$customer_cellphone			 	= "";
			$customer_fax				 	= "";
			$customer_password			 	= FgcmsCustomerBean::getCustomerPassword($this->db, $_SESSION['cust_id']);
			$customer_newsletter		 	= "";
			$customer_street_address		 	= "";
			$customer_postcode				 	= "";
			$customer_city					 	= "";
			$customer_state				 	= "";
			$customer_country_id		 		= "";
			$customer_company				 	= "";

			if(isset($_POST['customer_gender']))
				$customer_gender = $_POST['customer_gender'];
			if(isset($_POST['customer_firstname']))
				$customer_firstname = $_POST['customer_firstname'];
			if(isset($_POST['customer_lastname']))
				$customer_lastname = $_POST['customer_lastname'];
			if(isset($_POST['customer_dob']))
				$customer_dob = date('Y-m-d',strtotime($_POST['customer_dob']));
			if(isset($_POST['customer_email']))
				$customer_email = $_POST['customer_email'];
			if(isset($_POST['customer_telephone']))
				$customer_telephone = $_POST['customer_telephone'];
			if(isset($_POST['customer_cellphone']))
				$customer_cellphone = $_POST['customer_cellphone'];		
			if(isset($_POST['customer_fax']))
				$customer_fax = $_POST['customer_fax'];			
			if(isset($_POST['customer_password']) && $_POST['customer_password'] != "" && crypt($_POST['customer_password'], '$ntp$') != $customer_password)
				$customer_password = crypt($_POST['customer_password'], '$ntp$');
			if(isset($_POST['customer_newsletter']))
				$customer_newsletter = $_POST['customer_newsletter'];
				
			if(isset($_POST['delivery_gender']))
				$delivery_gender = $_POST['delivery_gender'];	
			if(isset($_POST['delivery_firstname']))
				$delivery_firstname = $_POST['delivery_firstname'];
			if(isset($_POST['delivery_lastname']))
				$delivery_lastname = $_POST['delivery_lastname'];	
			if(isset($_POST['delivery_street_address']))
				$delivery_street_address = $_POST['delivery_street_address'];
			if(isset($_POST['delivery_postcode']))
				$delivery_postcode = $_POST['delivery_postcode'];
			if(isset($_POST['delivery_city']))
				$delivery_city = $_POST['delivery_city'];
			if(isset($_POST['delivery_state']))
				$delivery_state = $_POST['delivery_state'];
			if(isset($_POST['delivery_country_id']))
				$delivery_country_id = $_POST['delivery_country_id'];
				
			if(isset($_POST['billing_gender']))
				$billing_gender = $_POST['billing_gender'];	
			if(isset($_POST['billing_firstname']))
				$billing_firstname = $_POST['billing_firstname'];
			if(isset($_POST['billing_lastname']))
				$billing_lastname = $_POST['billing_lastname'];	
			if(isset($_POST['billing_street_address']))
				$billing_street_address = $_POST['billing_street_address'];
			if(isset($_POST['billing_postcode']))
				$billing_postcode = $_POST['billing_postcode'];
			if(isset($_POST['billing_city']))
				$billing_city = $_POST['billing_city'];
			if(isset($_POST['billing_state']))
				$billing_state = $_POST['billing_state'];
			if(isset($_POST['billing_country_id']))
				$billing_country_id = $_POST['billing_country_id'];	
			if(isset($_POST['customer_company']))
				$customer_company = $_POST['customer_company'];
				
															
			
			$cust = array('customer_gender' => $customer_gender,
					  	  'customer_firstname' => $customer_firstname,
						  'customer_lastname' => $customer_lastname,
						  'customer_dob' => $customer_dob,
						  'customer_email' => $customer_email,
						  'customer_telephone' => $customer_telephone,
						  'customer_cellphone' => $customer_cellphone,
						  'customer_fax' => $customer_fax,
						  'customer_password' => $customer_password,
						  'customer_newsletter' => $customer_newsletter,
						  'delivery_gender' => $delivery_gender,
						  'delivery_firstname' => $delivery_firstname,
						  'delivery_lastname' => $delivery_lastname,
						  'delivery_street_address' => $delivery_street_address,
						  'delivery_postcode' => $delivery_postcode,
						  'delivery_city' => $delivery_city,
						  'delivery_state' => $delivery_state,
					 	  'delivery_country_id' => $delivery_country_id,
						  'billing_gender' => $billing_gender,
					 	  'billing_firstname' => $billing_firstname,
						  'billing_lastname' => $billing_lastname,
						  'billing_street_address' => $billing_street_address,
						  'billing_postcode' => $billing_postcode,
						  'billing_city' => $billing_city,
						  'billing_state' => $billing_state,
					 	  'billing_country_id' => $billing_country_id,
					  	  'customer_company' => $customer_company,
					  	  'customer_id' => $_SESSION['cust_id']);
			$ret = "";		
			$ret = $this->validateCustomer();
			if($ret['dem'] == 0) {					
				FgcmsCustomerBean::updateCustomer($this->db, $cust);
			} else {
				$type = array('ID' => -1, 'message' => $ret);
				echo array2json($type);
			}
			
	}
	
	public function activeCustomer() {
		
		if($_GET['code'] == crypt($customer_email, '$ntp$')){
			$cus = FgcmsCustomerBean::getActiveCustomerById($this->db, $_GET['id']);
			if($cus->customer_active != '1'){
				FgcmsCustomerBean::activeAccount($this->db, $_GET['id']);
				$return = "1";
			}else{
				$return = "2";
			}
		}else{
			$return = '0';
			$return = "Invalid link!";
		}
		$this->smarty->assign("RETURN",$return);
	}
	
	public function forgotPassword() {
		$msg = "";
		if(isset($_POST['email']) && $_POST['email'] != ""){
			$msg = "";
			$email = $_POST['email'];
			$check = $this->check_email_address($email);
			if($check == false){
				$msg .= "Invalid email address.";
			}else{
				$exists = FgcmsCustomerBean::ExistsByEmail($this->db,$email);
				if($exists == "")
				{
					$msg .= "This email address not exists.";
				}
			}
		}else{
			$msg .= "Invalid email address.";
		}
		if($msg == ""){
			$time = time();
			$get_code = md5($time); // tao 6 so ngau nhien
			$pass= substr($get_code, 0, 10);
			$passcrypt = crypt($pass, '$ntp$');
			FgcmsCustomerBean::updatePassword($this->db, $email, $passcrypt);
			
			$type = array('ID' => 0);
			$host = $_SERVER['HTTP_HOST'].FOLDER_HOST;
			$code = crypt($to, '$ntp$');
			$from = FROMMAIL;
			$fromname = "T & L Tec GdbR";
			$subject = "Your new password for GunWebshop";
			$message = 	"Hello,<br /><br />" .
						"Your Login: $email<br />" .
						"Your Password: $pass<br /><br /><br />" .
						"Sincerely,<br /><br />" . 
						"T & L Tec GdbR<br />" .
						"Plattlinger Strasse 5a<br />" .
						"93055 Regensburg<br />" .
						"Email: info@guncms.de<br />" .
						"Germany";
			$header = "Content-type: text/html\r\nFrom: $from\r\nReply-to: $from";
			FgcmsEmail::send_mail($_POST['email'], $message, $subject, $from, $fromname, false);
			header("Location: ./?page=customer_send_access");
		}else{
			setcookie("message", $msg,time()+10);
			header("Location: ./?page=customer_forgot_password");
		}	
	}
	
	/**
	 * @brief check validation of Customer information
	 * @retval error_message
	 * @date 2008/08/07
	**/
	public function checkValidDate($mydate) 
	{
    	list($yy,$mm,$dd)=explode("-",$mydate);
    	if (is_numeric($yy) && is_numeric($mm) && is_numeric($dd))
    	{
       	 return checkdate($mm,$dd,$yy);
    	}
    	return false;           
	}
	
	public function check_email_address($email) {
    	// First, we check that there's one @ symbol, and that the lengths are right
    	if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
    	// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
    	return false;
    	}
    	// Split it into sections to make life easier
    	$email_array = explode("@", $email);
    	$local_array = explode(".", $email_array[0]);
   		for ($i = 0; $i < sizeof($local_array); $i++) {
   			if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
   			return false;
   			}
   		}
   		if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1]))  // Check if domain is IP. If not, it should be valid domain name
   		{
   			$domain_array = explode(".", $email_array[1]);
   			if (sizeof($domain_array) < 2) 
   			{
   			return false; // Not enough parts to domain
   			}
   			for ($i = 0; $i < sizeof($domain_array); $i++) 
   			{
   				if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) 
   				{
   				return false;
   				}
   			}
   		}
   		return true;
	}
	
	public function validateCustomer()
    {
    	$msg = array();
		$msg["customer_gender"] = '';
		/*$msg["customer_firstname"] = '';
		$msg["customer_lastname"] = '';*/
		//$msg["customer_dob"] = '';
		$msg["billing_gender"] = '';
		$msg["billing_firstname"] = '';
		$msg["billing_lastname"] = '';
		$msg["billing_street_address"] = '';
		$msg["billing_postcode"] = '';
		$msg["billing_city"] = '';
		$msg["billing_state"] = '';
		$msg["billing_country_id"] = '';
		$msg["delivery_gender"] = '';
		$msg["delivery_firstname"] = '';
		$msg["delivery_lastname"] = '';
		$msg["delivery_street_address"] = '';
		$msg["delivery_postcode"] = '';
		$msg["delivery_city"] = '';
		$msg["delivery_state"] = '';
		$msg["delivery_country_id"] = '';
		$msg["customer_telephone"] = '';
		$msg["customer_fax"] = '';
		$msg["customer_email"] = '';
		$msg["customer_password"] = '';
		$msg["customer_confirmpassword"] = '';
		$dem =0;
		if(isset($_POST['customer_gender']) && $_POST['customer_gender'] == ""){
			$msg["customer_gender"] = FgcmsMultiLanguageBean::changelang('ERR_GENDER');//"Please select your Gender.";
			$dem++;
		}
		/*if(isset($_POST['customer_firstname']) && $_POST['customer_firstname'] == ""){
			$msg["customer_firstname"] = FgcmsMultiLanguageBean::changelang('ERR_FIRSTNAME');//"Your first name could not be empty.";
			$dem++;
		}
		if(isset($_POST['customer_lastname']) && $_POST['customer_lastname'] == ""){
			$msg["customer_lastname"]= FgcmsMultiLanguageBean::changelang('ERR_LASTNAME');//"Your last name could not be empty.";
			$dem++;
		}*/
		/*
		if($_POST['customer_dob'] == ""){
			$msg["customer_dob"]= "Your date of birth could not be empty.";
			$dem++;
		}else{
			$flag = $this->checkValidDate(date('Y-m-d',strtotime($_POST['customer_dob'])));
			if($flag == false){
				$msg["customer_dob"]= "Invalid date of birth.";
				$dem++;
			}else{
				$yob = date('Y-m-d',strtotime($_POST['customer_dob'])) + 0;
				$currentyear = date("Y-m-d") + 0;
				if($yob > $currentyear - 10){
					$msg["customer_dob"]= "You must be enough ten years old.";
					$dem++;
				}
			}
		}*/
		if(isset($_POST['billing_gender']) && $_POST['billing_gender'] == ""){
			$msg["billing_gender"] = FgcmsMultiLanguageBean::changelang('ERR_GENDER');//"Please select your Gender.";
			$dem++;
		}
		if(isset($_POST['billing_firstname']) && $_POST['billing_firstname'] == ""){
			$msg["billing_firstname"] = FgcmsMultiLanguageBean::changelang('ERR_FIRSTNAME');//"Your first name could not be empty.";
			$dem++;
		}
		if(isset($_POST['billing_lastname']) && $_POST['billing_lastname'] == ""){
			$msg["billing_lastname"]= FgcmsMultiLanguageBean::changelang('ERR_LASTNAME');//"Your last name could not be empty.";
			$dem++;
		}
		if(isset($_POST['billing_street_address']) && $_POST['billing_street_address'] == ""){
			$msg["billing_street_address"]= FgcmsMultiLanguageBean::changelang('ERR_BILLING_ADDRESS');//"Your street address could not be empty.";
			$dem++;
		}
		if(isset($_POST['billing_postcode']) && $_POST['billing_postcode'] == ""){
			$msg["billing_postcode"]= FgcmsMultiLanguageBean::changelang('ERR_BILLING_POSTCODE');//"Your postcode could not be empty.";
			$dem++;
		}
		if(isset($_POST['billing_postcode']) && $_POST['billing_postcode'] != "" && (!is_numeric($_POST['billing_postcode']) || strlen($_POST['billing_postcode']) < 4)){
			$msg["billing_postcode"]= FgcmsMultiLanguageBean::changelang('ERR_BILLING_POSTCODE_INVALID');//"Invalid postcode.";
			$dem++;
		}
		if(isset($_POST['billing_city']) && $_POST['billing_city'] == ""){
			$msg["billing_city"]= FgcmsMultiLanguageBean::changelang('ERR_BILLING_CITY');//"Your city could not be empty.";
			$dem++;
		}
		if(isset($_POST['billing_state']) && $_POST['billing_state'] == ""){
			$msg["billing_state"]= FgcmsMultiLanguageBean::changelang('ERR_BILLING_STATE');//"Your state/province could not be empty.";
			$dem++;
		}
		if(isset($_POST['billing_country_id']) && $_POST['billing_country_id'] == ""){
			$msg["billing_country_id"]= FgcmsMultiLanguageBean::changelang('ERR_BILLING_COUNTRY');//"Please select your country.";
			$dem++;
		}
		if(isset($_POST['delivery_gender']) && $_POST['delivery_gender'] == ""){
			$msg["delivery_gender"] = FgcmsMultiLanguageBean::changelang('ERR_GENDER');//"Please select your Gender.";
			$dem++;
		}
		if(isset($_POST['delivery_firstname']) && $_POST['delivery_firstname'] == ""){
			$msg["delivery_firstname"] = FgcmsMultiLanguageBean::changelang('ERR_FIRSTNAME');//"Your first name could not be empty.";
			$dem++;
		}
		if(isset($_POST['delivery_lastname']) && $_POST['delivery_lastname'] == ""){
			$msg["delivery_lastname"]= FgcmsMultiLanguageBean::changelang('ERR_LASTNAME');//"Your last name could not be empty.";
			$dem++;
		}
		if(isset($_POST['delivery_street_address']) && $_POST['delivery_street_address'] == ""){
			$msg["delivery_street_address"]= FgcmsMultiLanguageBean::changelang('ERR_DELIVERY_ADDRESS');//"Your street address could not be empty.";
			$dem++;
		}
		if(isset($_POST['delivery_postcode']) && $_POST['delivery_postcode'] == ""){
			$msg["delivery_postcode"]= FgcmsMultiLanguageBean::changelang('ERR_DELIVERY_POSTCODE');//"Your postcode could not be empty.";
			$dem++;
		}
		if(isset($_POST['delivery_postcode']) && $_POST['delivery_postcode'] != "" && (!is_numeric($_POST['delivery_postcode']) || strlen($_POST['delivery_postcode']) < 4)){
			$msg["delivery_postcode"]= FgcmsMultiLanguageBean::changelang('ERR_DELIVERY_POSTCODE_INVALID');//"Invalid postcode.";
			$dem++;
		}
		if(isset($_POST['delivery_city']) && $_POST['delivery_city'] == ""){
			$msg["delivery_city"]= FgcmsMultiLanguageBean::changelang('ERR_DELIVERY_CITY');//"Your city could not be empty.";
			$dem++;
		}
		if(isset($_POST['delivery_state']) && $_POST['delivery_state'] == ""){
			$msg["delivery_state"]= FgcmsMultiLanguageBean::changelang('ERR_DELIVERY_STATE');//"Your state/province could not be empty.";
			$dem++;
		}
		if(isset($_POST['delivery_country_id']) && $_POST['delivery_country_id'] == ""){
			$msg["delivery_country_id"]= FgcmsMultiLanguageBean::changelang('ERR_DELIVERY_COUNTRY');//"Please select your country.";
			$dem++;
		}
		if(isset($_POST['customer_telephone']) && $_POST['customer_telephone'] == ""){
			$msg["customer_telephone"]= FgcmsMultiLanguageBean::changelang('ERR_TELEPHONE');//"Your telephone number could not be empty.";
			$dem++;
		}
		if(isset($_POST['customer_telephone']) && $_POST['customer_telephone'] != "" && (strlen($_POST['customer_telephone']) < 6 || (!preg_match('/^[\(]?(\d{0,3})[\)]?[\s]?[\-]?(\d{3})[\s]?[\-]?(\d{4})[\s]?[x]?(\d*)$/', $_POST['customer_telephone'])))){
			$msg["customer_telephone"]= FgcmsMultiLanguageBean::changelang('ERR_TELEPHONE_INVALID');//"Invalid telephone number.";
			$dem++;
		}
		if(isset($_POST['customer_fax']) && $_POST['customer_fax'] != "" && !is_numeric($_POST['customer_fax'])){
			$msg["customer_fax"]= FgcmsMultiLanguageBean::changelang('ERR_FAX');//"Invalid fax number.";
			$dem++;
		}
		if(isset($_POST['customer_email']) && $_POST['customer_email'] == ""){
			$msg["customer_email"]= FgcmsMultiLanguageBean::changelang('ERR_CUSTOMER_EMAIL');//"Your email address could not be empty.";
			$dem++;
		}
		if(isset($_POST['customer_email']) && $_POST['customer_email'] != ""){
			$flag = $this->check_email_address($_POST['customer_email']);
			if($flag == false){
				$msg["customer_email"]= FgcmsMultiLanguageBean::changelang('ERR_CUSTOMER_EMAIL_INVALID');//"Invalid email address.";
				$dem++;
			}else{
				if(isset($_SESSION['cust_id'])){
					$customer_email = FgcmsCustomerBean::getCustomerEmail($this->db, $_SESSION['cust_id']);
					if($_POST['customer_email'] != $customer_email)
					{
						$email = FgcmsCustomerBean::getAllEmail($this->db);
						if($email!=""){
							foreach ($email as $item){
								if($item->customer_email == $_POST['customer_email']){	
									$msg["customer_email"]= FgcmsMultiLanguageBean::changelang('ERR_CUSTOMER_EMAIL_EXISTS');//"This email address already exists.";
									$dem++;
								} 
							}
						}
					}
				}else{
						$email = FgcmsCustomerBean::getCustomerByEmail($this->db,$_POST['customer_email']);
						if($email!=""){
							$msg["customer_email"]= FgcmsMultiLanguageBean::changelang('ERR_CUSTOMER_EMAIL_EXISTS');//"This email address already exists.";
							$dem++;
						}
				}
			}
			
		}
		if(isset($_SESSION['cust_id']))
		{
			if(isset($_POST['customer_password']) && $_POST['customer_password'] != "")
			{
				if(strlen($_POST['customer_password']) <5){
					$msg["customer_password"]= FgcmsMultiLanguageBean::changelang('ERR_PASSWORD_ATLEAST');//"Your password must contain at least 5 characters.";
					$dem++;
				}
				$customer_password = FgcmsCustomerBean::getCustomerPassword($this->db, $_SESSION['cust_id']);
				if(crypt($_POST['customer_password'], '$ntp$') != $customer_password){
					if($_POST['customer_confirmpassword'] == ""){
						$msg["customer_password"]= FgcmsMultiLanguageBean::changelang('ERR_PASSWORD');//"Your password confirmation could not be empty.";
						$dem++;
					}
					if($_POST['customer_confirmpassword'] != "" && $_POST['customer_confirmpassword'] != $_POST['customer_password']){
						$msg["customer_confirmpassword"]= FgcmsMultiLanguageBean::changelang('ERR_PASSWORD_CONFIRM_NOTMATCH');//"Your password confirmation must match your password.";	
						$dem++;
					}
				}
			}
		}
		else
		{
			if(!$_SESSION['u_isadmin'])
			{
				if($_POST['customer_password'] == ""){
					$msg["customer_password"]= FgcmsMultiLanguageBean::changelang('ERR_PASSWORD');//"Your password could not be empty.";
					$dem++;
				}
				if($_POST['customer_password'] != "" && strlen($_POST['customer_password']) <5){
					$msg["customer_password"]= FgcmsMultiLanguageBean::changelang('ERR_PASSWORD_ATLEAST');//"Your password must contain at least 5 characters.";
					$dem++;
				}	
				if($_POST['customer_confirmpassword'] == ""){
					$msg["customer_confirmpassword"]= FgcmsMultiLanguageBean::changelang('ERR_PASSWORD_CONFIRM');//"Your password confirmation could not be empty.";
					$dem++;
				}
				if($_POST['customer_confirmpassword'] != "" && $_POST['customer_confirmpassword'] != $_POST['customer_password']){
					$msg["customer_confirmpassword"]= FgcmsMultiLanguageBean::changelang('ERR_PASSWORD_CONFIRM_NOTMATCH');//"Your password confirmation must match your password.";	
					$dem++;
				}
			}
		}
		$msg["dem"] = $dem;
		return $msg;
    }
}
?>
